// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: 'zbdc-6gavbhj633c62e27'
})
const db = cloud.database();
const _ = db.command

// 查询已上传的单词
exports.main = async (event, context) => {
  let cet4 = await db.collection('CET4')
  .where({
    address:_.exists(true),
    lyric:_.exists(true)
    })
  .field({
    _id: true,
    content:true
  })
  .get()
  let cet6 = await db.collection('CET6')
  .where({
    address:_.exists(true),
    lyric:_.exists(true)
  })
  .field({
    _id: true,
    headWord:true
  })
  .get()
  let kaoyan = await db.collection('KAOYAN')
  .where({
    address:_.exists(true),
    lyric:_.exists(true)
  })
  .field({
    _id: true,
    headWord:true
  })
  .get()
  new Promise(() => {
    cet4.data.forEach(item => {
      item.type = 'cet4'
    });
    cet6.data.forEach(item => {
      item.type = 'cet6'
    });
    kaoyan.data.forEach(item => {
      item.type = 'PG'
    });
  })
  return [...cet4.data, ...cet6.data, ...kaoyan.data]
}